package org.osll.tictactoe.client.gui;

import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JOptionPane;
import javax.swing.event.ChangeListener;

import org.osll.tictactoe.Control;
import org.osll.tictactoe.Move;
import org.osll.tictactoe.ServerConnection;
import org.osll.tictactoe.Team;
import org.osll.tictactoe.transport.ConnectionFactory;
import org.osll.tictactoe.transport.DefaultOptions;
import org.osll.tictactoe.transport.ConnectionFactory.Type;

public class GuiClient {
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		HashMap<String, Type> m = new HashMap<String, Type>();
		m.put("TCP",Type.TCP);
		m.put("UDP",Type.UDP);
		m.put("RMI",Type.RMI);
		m.put("CORBA",Type.CORBA);
		
		Type connectionType = null;
		ServerConnection con = null;
		Control control = null;
		String inputUser = null;
		Team team = null;
		
		Object[] possibilities = {"TCP","UDP","RMI","CORBA"};
		String s = (String)JOptionPane.showInputDialog(
		                    null,
		                    "Выбирете способ соединения с сервером:",
		                    "Выбор способа соединения",
		                    JOptionPane.PLAIN_MESSAGE,
		                    null,
		                    possibilities,
		                    "TCP");
		if(s!=null) {
			connectionType = m.get(s);
		}
		
		String host = (String)JOptionPane.showInputDialog(
                null,
                "Введите адрес сервера:",
                "Ввод адреса сервера",
                JOptionPane.QUESTION_MESSAGE,
                null,
                null,
                "127.0.0.1");
		if(host!=null) {
			inputUser = (String)JOptionPane.showInputDialog(
                    null,
                    "Введите имя пользователя:",
                    "Ввод имени пользователя",
                    JOptionPane.QUESTION_MESSAGE,
                    null,
                    null,
                    "");
			if(inputUser!=null) {
				HashMap<String, Team> mt = new HashMap<String, Team>();
				mt.put("Крестики",Team.X);
				mt.put("Нолики",Team.O);
				
				Object[] teamPossibilities = {"Крестики","Нолики"};
				s = (String)JOptionPane.showInputDialog(
				                    null,
				                    "Выбирете команду за которую вы хотите играть:",
				                    "Выбор команды",
				                    JOptionPane.PLAIN_MESSAGE,
				                    null,
				                    teamPossibilities,
				                    "Крестики");
				
				if(s!=null) {
					 team = mt.get(s);
				}
				DefaultOptions.getInstance().setHost(host);
				con = ConnectionFactory.getInstance().getConnection(connectionType);
				control = con.connect(inputUser, team);
				
			}
		}
		MainFrame mainframe = new MainFrame(control,team,inputUser);
		mainframe.setVisible(true);
	}

}
